package com.ysh.smartplatform.utils;

import com.ysh.smartplatform.pojo.BaseUser;
import com.ysh.smartplatform.pojo.SysUserInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 袁胜虎 on 2022/9/6 21:38
 */
public class SecurityUtils {

    public static BaseUser getLoginUser() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            Object principal = auth.getPrincipal();
            if (principal instanceof BaseUser)
                return (BaseUser) principal;
        }

        return null;
    }
    /**
     * 获取当前登录的用户
     * @return
     */
//    public static SysUserInfo getLoginUser() {
//        Object user = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession().getAttribute("user");
//        return (SysUserInfo) user;
//    }
}
